import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router);

const router = new Router({
    "mode": "history",
    routes: [
        {
            path: "/",
            //redirect: "/student/login"
            redirect: "/admin/login"
        },
        {
            path: "/admin/login",
            name: "AdminLogin",
            component: () => import("./views/admin/AdminLogin.vue"),
        },
        {
            path: "/admin/index",
            name: "AdminIndex",
            component: () => import("./views/admin/AdminIndex.vue"),
            meta: { identity: "admin" },
            children: [
                {
                    path: "/admin/index",
                    name: "Home",
                    component: () => import("./views/admin/AdminHome.vue"),
                    meta: { identity: "admin" },
                },
                {
                    path: "/admin/info",
                    name: "AdminUserInfo",
                    component: () => import("./views/admin/AdminUserInfo.vue"),
                    meta: { identity: "admin" }
                },
                {
                    path: "/admin/student/info",
                    name: "StudentInfo",
                    component: () => import("./views/admin/StudentInfo.vue"),
                    meta: { identity: "admin" }
                },
                {
                    path: "/admin/change/pw",
                    name: "ChangeAdminPw",
                    component: () => import("./views/admin/ChangeAdminPw.vue"),
                    meta: { identity: "admin" }
                },
            ]
        },

        {
            path: "/student/login",
            name: "StudentLogin",
            component: () => import("./views/student/StudentLogin")
        },
        {
            path: "/student/index",
            name: "StudentIndex",
            component: () => import("./views/student/StudentIndex"),
            meta: { identity: "student" },
            children: [
                {
                    path: "/student/info",
                    name: "StudentInfo",
                    component: () => import("./views/student/StudentInfo"),
                    meta: { identity: "student" }
                },
                {
                    path: "/student/index",
                    name: "StudentHome",
                    component: () => import("./views/student/StudentHome"),
                    meta: { identity: "student" }
                },
                {
                    path: "/student/change/password",
                    name: "StudentChangePassword",
                    component: () => import("./views/student/StudentChangPassword"),
                    meta: { identity: "student" }
                },
                {
                    path: "/student/update/info",
                    name: "StudentUpdateInfo",
                    component: () => import("./views/student/StudentUpdateInfo"),
                    meta: { identity: "student" }
                },
            ]
        },
        {
            path: "*",
            name: "NotFound",
            component: () => import("./views/admin/NotFound.vue")
        }
    ]
});


//vue.js前置守卫
router.beforeEach((to, from, next) => {
    if (!localStorage.getItem("token") && (to.meta.identity === "admin" || to.meta.identity === "student")) {
        switch (to.meta.identity) {
            case "admin":
                return next("/admin/login");
            case "student":
                return next("/student/login");
        }
    }
    next();
});

export default router;
